/*
 * Created on 07.10.2006 by leck
 *
 */
package de.devisnik.eidle;

import junit.framework.TestCase;
import de.devisnik.eidle.screen.IScreen;

public class ScreenTogglerTest extends TestCase {

	private IScreen[] createScreens(final int number) {
		final IScreen[] screens = new IScreen[number];
		for (int i = 0; i < screens.length; i++) {
			screens[i] = new NullScreen();
		}
		return screens;
	}

	private AbstractScreenToggler createToggler(final IScreen[] screens,
			final IScreenHandler handler) {
		return new AbstractScreenToggler(screens, handler) {
			protected boolean isScreenEnabled(final IScreen screen) {
				return true;
			}
		};
	}

	public void testDispose() {
		final int[] screen1Disposed = { 0 };
		final NullScreen screen1 = new NullScreen() {

			// @Override
			public void dispose() {
				screen1Disposed[0]++;
			}
		};
		final int[] screen2Disposed = { 0 };
		final NullScreen screen2 = new NullScreen() {

			// @Override
			public void dispose() {
				screen2Disposed[0]++;
			}

		};
		final AbstractScreenToggler toggler = createToggler(new IScreen[] {
				screen1, screen2 }, new SimpleScreenHandler());
		toggler.dispose();
		assertEquals(1, screen1Disposed[0]);
		assertEquals(1, screen2Disposed[0]);

	}

	public void testIsAtLast() {
		final IScreen[] screens = createScreens(2);
		final AbstractScreenToggler screenToggler = createToggler(screens,
				new SimpleScreenHandler());
		screenToggler.nextScreen();
		assertEquals(false, screenToggler.isLast());
		screenToggler.nextScreen();
		assertEquals(true, screenToggler.isLast());

	}

	public void testNextScreen() {
		final IScreen[] screens = createScreens(2);
		final boolean[] isActivateCalled = { false, false };
		final SimpleScreenHandler nullScreenHandler = new SimpleScreenHandler() {

			// @Override
			public void activate(IScreen screen) {
				super.activate(screen);
				if (screen == screens[0]) {
					isActivateCalled[0] = true;
				} else if (screen == screens[1]) {
					isActivateCalled[1] = true;
				}
			}

		};
		final AbstractScreenToggler toggler = createToggler(screens,
				nullScreenHandler);
		toggler.nextScreen();
		assertEquals(true, isActivateCalled[0]);

		toggler.nextScreen();
		assertEquals(true, isActivateCalled[1]);
	}

	public void testPreviousScreen() {
		final IScreen[] screens = createScreens(2);
		final boolean[] isActivateCalled = { false, false };
		final SimpleScreenHandler nullScreenHandler = new SimpleScreenHandler() {

			// @Override
			public void activate(IScreen screen) {
				super.activate(screen);
				if (screen == screens[0]) {
					isActivateCalled[0] = true;
				} else if (screen == screens[1]) {
					isActivateCalled[1] = true;
				}
			}

		};
		final AbstractScreenToggler toggler = createToggler(screens,
				nullScreenHandler);
		toggler.previousScreen();
		assertEquals(true, isActivateCalled[1]);

		toggler.previousScreen();
		assertEquals(true, isActivateCalled[0]);
	}
}
